package com.pur.zsms.check.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.sf.cglib.beans.BeanCopier;

import org.springframework.stereotype.Service;

import com.pur.zsms.check.dao.ICheckDao;
import com.pur.zsms.check.service.ICheckService;
import com.pur.zsms.check.vo.CheckVo;
import com.pur.zsms.check.vo.CheckVoForSalesVolume;
import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.matermanage.mater.dao.IMaterDao;
import com.pur.zsms.matermanage.mater.po.MaterLevelCblPo;
import com.pur.zsms.matermanage.mater.po.MaterPo;
import com.pur.zsms.matermanage.mater.vo.MaterValueVo;
import com.pur.zsms.matermanage.mater.vo.MaterVo;
import com.pur.zsms.ordercustommanage.dao.ISelectFoodDao;
import com.pur.zsms.ordercustommanage.po.OrderSupplierMaterRealPo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierPo;
import com.pur.zsms.ordercustommanage.vo.OrderSupplierMaterRealVo;
import com.pur.zsms.ordercustommanage.vo.SelectMaterVo;
import com.pur.zsms.supplier.dao.SupplierDao;
import com.pur.zsms.supplier.dao.SupplierOrderDao;
import com.pur.zsms.supplier.po.SupplierPo;
import com.pur.zsms.systemsetting.datadictionary.dao.IDataDictionaryDao;
import com.pur.zsms.systemsetting.datadictionary.po.Code;
import com.pur.zsms.unusual.dao.IUnusualDao;
import com.pur.zsms.unusual.po.OrderSupplierMaterUnusualPo;
import com.pur.zsms.unusual.service.IUnusualService;
import com.pur.zsms.unusual.vo.OrderSupplierMaterUnusualVo;

@Service("checkService")
public class CheckServiceImpl implements ICheckService {
	String msg = "";

	@Resource(name = "dataDictionaryDao")
	private IDataDictionaryDao dataDictionaryDao;

	@Resource(name = "checkDao")
	private ICheckDao checkDao;

	@Resource(name = "materDao")
	private IMaterDao materDaoImpl;

	@Resource(name = "supplierDao")
	private SupplierDao supplierDao;

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public ICheckDao getCheckDao() {
		return checkDao;
	}

	public void setCheckDao(ICheckDao checkDao) {
		this.checkDao = checkDao;
	}

	public PaginationSupport listCheck(PageController pageController, CheckVo checkVo) throws GenericException {
		return checkDao.listCheck(pageController, checkVo);
	}

	public PaginationSupport listCheckForSupplier(PageController pageController, CheckVo checkVo) throws GenericException {
		return checkDao.listCheckForSupplier(pageController, checkVo);
	}

	public PaginationSupport findCheckDetail(PageController pageController, CheckVo checkVo) throws GenericException {
		return checkDao.findCheckDetail(pageController, checkVo);
	}

	public List<Object> getSumValueForCheck(CheckVo checkVo) throws GenericException {
		return checkDao.getSumValueForCheck(checkVo);
	}

	public List<Object[]> getListCheckForExcel(CheckVo checkVo) throws GenericException {
		return checkDao.getListCheckForExcel(checkVo);
	}

	public List<Object[]> getListCheckForExcelDetail(CheckVo checkVo) throws GenericException {
		return checkDao.getListCheckForExcelDetail(checkVo);
	}

	public CheckVo getSalesVolume(CheckVo checkVo) throws GenericException {
		Code code = dataDictionaryDao.findCodeByCodeId(checkVo.getTimeType(), "time_type");
		checkVo.setTimeTypeString(code.getCodeName());
		List<Object[]> listTotal = checkDao.getSalesVolume(checkVo);
		if (listTotal != null && listTotal.size() > 0) {
			// List<CheckVoForSalesVolume> listCheckVoForSalesVolume = new
			// ArrayList<CheckVoForSalesVolume>();

			StringBuffer jsonUserTestValue = new StringBuffer("");
			jsonUserTestValue.append("[");
			for (int i = 0; i < listTotal.size(); i++) {
				Object[] a = listTotal.get(i);
				/*
				 * CheckVoForSalesVolume b =new CheckVoForSalesVolume();
				 * b.setX1(Double.valueOf(a[3].toString()));
				 * b.setY1(Double.valueOf(a[0].toString()));
				 * listCheckVoForSalesVolume.add(b);
				 */

				jsonUserTestValue.append("{\"");
				jsonUserTestValue.append("value");
				jsonUserTestValue.append("\":\"");
				jsonUserTestValue.append(a[3].toString());
				jsonUserTestValue.append("\",\"text\":\"");
				jsonUserTestValue.append(a[0].toString());
				jsonUserTestValue.append("\"},");
			}
			jsonUserTestValue = new StringBuffer(jsonUserTestValue.substring(0, jsonUserTestValue.length() - 1));
			jsonUserTestValue.append("]");
			checkVo.setSalesVolume(jsonUserTestValue.toString());

			MaterPo materPo = materDaoImpl.getMaterPoByMaterId(checkVo.getMaterId()).get(0);
			checkVo.setMaterLevel(1);
			checkVo.setMaterName(materPo.getMaterName());
			checkVo.setUnit(materPo.getUnit());
			SupplierPo supplierPo = supplierDao.getSupPoBySupId(String.valueOf(checkVo.getSupId()));
			checkVo.setSupName(supplierPo.getCompanyName());
		}

		return checkVo;
	}

	public List<Object[]> getSalesVolumeForExcel(CheckVo checkVo) throws GenericException {
		return checkDao.getSalesVolumeForExcel(checkVo);
	}

}
